<template>
  <el-drawer direction="rtl" v-model="dialogVisible" :title="dialogTitle" size="70%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="日期区间" prop="loginDate">
          <el-date-picker
            v-model="queryParams.dayScope"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
            end-placeholder="结束日期"
            start-placeholder="开始日期"
            type="daterange"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 总计 -->
    <ContentWrap>
      <el-descriptions :column="3" label-class-name="desc-label">
        <el-descriptions-item label="收入">
          {{ divide(summer.income, 100) }}
        </el-descriptions-item>
        <el-descriptions-item label="收入总手续费">{{
          divide(summer.commissOfIncome, 100)
        }}</el-descriptions-item>
        <el-descriptions-item label="收入总到账"
          ><el-tag size="mini" type="success">{{
            divide(summer.allIncome, 100)
          }}</el-tag></el-descriptions-item
        >
        <el-descriptions-item label="退款">{{ divide(summer.refund, 100) }} </el-descriptions-item>
        <el-descriptions-item label="退手续费">{{
          divide(summer.commissOfRefund, 100)
        }}</el-descriptions-item>
        <el-descriptions-item label="总退账"
          ><el-tag size="mini" type="danger">{{
            divide(summer.allRefund, 100)
          }}</el-tag></el-descriptions-item
        >
        <el-descriptions-item label="净收入">
          <el-tag size="mini" type="success">{{ divide(summer.resultIncome, 100) }}</el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </ContentWrap>
    <!-- 分割线 -->
    <el-divider />

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="时间" align="center" prop="calcDay" />
        <el-table-column label="收入" align="center">
          <template #default="scope">
            {{ divide(scope.row.income, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="收入总手续费" align="center">
          <template #default="scope">
            {{ divide(scope.row.commissOfIncome, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="收入总到账" align="center">
          <template #default="scope">
            {{ divide(scope.row.allIncome, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="退款" align="center">
          <template #default="scope">
            {{ divide(scope.row.refund, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="退手续费" align="center">
          <template #default="scope">
            {{ divide(scope.row.commissOfRefund, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="总退账" align="center">
          <template #default="scope">
            {{ divide(scope.row.allRefund, 100) }}
          </template>
        </el-table-column>
        <el-table-column label="净收入" align="center">
          <template #default="scope">
            {{ divide(scope.row.resultIncome, 100) }}
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </el-drawer>
</template>
<script lang="ts" setup>
import { divide } from '@/utils/tools'
import {
  getPayElecDayReportSummerOfHouse,
  getPayElecDayReportPageOfHouse
} from '@/api/zwsj/payElecDayReport'

defineOptions({ name: 'HousePayElecDayReport' })

const loading = ref(false) // 列表的加载中
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据
const summer = ref({}) // 总览数据

/** 打开弹窗 */
const open = async (houseId: number) => {
  dialogVisible.value = true
  dialogTitle.value = '房产电费收支情况'

  //设置区域默认值
  queryParams.houseId = houseId
  //handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dayScope: [],
  startDay: undefined,
  endDay: undefined,
  houseId: undefined
})

const queryFormRef = ref() // 搜索的表单

/** 查询总览 */
const getSummer = async () => {
  try {
    const data = await getPayElecDayReportSummerOfHouse(queryParams)
    summer.value = data
  } finally {
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getPayElecDayReportPageOfHouse(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1

  queryParams.startDay = queryParams.dayScope[0]
  queryParams.endDay = queryParams.dayScope[1]
  getSummer()
  getList()
}
</script>
